home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / overview / example / guiwindow.000 < prev    next >
Encoding:
Text File  |  1996-02-26  |  4.3 KB  |  165 lines

  1. <!--NewPage-->
  2. <html>
  3. <head>
  4. <title>GUIWindow.java</title>
  5. </head>
  6. <body>
  7. <p>
  8. <hr size=4>
  9.  
  10. <h2>
  11.     GUIWindow.java
  12. </h2>
  13. <p>
  14. <blockquote>
  15.  
  16. <pre>
  17.  
  18. /*
  19.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software
  22.  * and its documentation for NON-COMMERCIAL purposes and without
  23.  * fee is hereby granted provided that this copyright notice
  24.  * appears in all copies. Please refer to the file "copyright.html"
  25.  * for further important copyright and licensing information.
  26.  *
  27.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  28.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  29.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  30.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  31.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  32.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  33.  */
  34. import java.awt.*;
  35.  
  36. public class GUIWindow extends Frame {
  37.     boolean inAnApplet = true;
  38.     final String FILEDIALOGMENUITEM = "File dialog...";
  39.  
  40.     public GUIWindow() {
  41.     Panel bottomPanel = new Panel();
  42.     Panel centerPanel = new Panel();
  43.  
  44.     MenuBar mb = new MenuBar();
  45.     Menu m = new Menu("Menu");
  46.     m.add(new MenuItem("Menu item 1"));
  47.     m.add(new CheckboxMenuItem("Menu item 2"));
  48.     m.add(new MenuItem("Menu item 3"));
  49.     m.add(new MenuItem("-"));
  50.     m.add(new MenuItem(FILEDIALOGMENUITEM));
  51.     mb.add(m);
  52.     setMenuBar(mb);
  53.  
  54.     //Add small things at the bottom.
  55.     bottomPanel.add(new TextField("TextField"));
  56.     bottomPanel.add(new Button("Button"));
  57.     bottomPanel.add(new Checkbox("Checkbox"));
  58.     Choice c = new Choice();
  59.     c.addItem("Choice Item 1");
  60.     c.addItem("Choice Item 2");
  61.     c.addItem("Choice Item 3");
  62.     bottomPanel.add(c);
  63.  
  64.     //Add big things to the center area.
  65.     centerPanel.setLayout(new GridLayout(1,2));
  66.     //Put a canvas in the left column.
  67.     centerPanel.add(new MyCanvas());
  68.     //Put a label and a text area in the right column.
  69.     Panel p = new Panel();
  70.     p.setLayout(new BorderLayout());
  71.     p.add("North", new Label("Label", Label.CENTER));
  72.     p.add("Center", new TextArea("TextArea", 5, 20));
  73.     centerPanel.add(p);
  74.  
  75.     setLayout(new BorderLayout());
  76.     add("South", bottomPanel);
  77.     add("Center", centerPanel);
  78.  
  79.     //Put a list in the window.
  80.     List l = new List(3, false);
  81.     l.addItem("List item 1");
  82.     l.addItem("List item 2");
  83.     l.addItem("List item 3");
  84.     l.addItem("List item 4");
  85.     l.addItem("List item 5");
  86.     l.addItem("List item 6");
  87.     l.addItem("List item 7");
  88.     l.addItem("List item 8");
  89.     l.addItem("List item 9");
  90.     l.addItem("List item 10");
  91.     add("East", l); 
  92.     }
  93.  
  94.     public boolean handleEvent(Event evt) {
  95.     if ((evt.id == Event.ACTION_EVENT) 
  96.         && (evt.target instanceof MenuItem)) {
  97.         String label = (String)evt.arg;
  98.         if (label.equals(FILEDIALOGMENUITEM)) {
  99.         FileDialog fd = new FileDialog(this, "FileDialog");
  100.         fd.show();
  101.         }
  102.         return true;
  103.     } 
  104.         if (evt.id == Event.WINDOW_ICONIFY) {//DOESN'T seem to be necessary
  105.             hide();
  106.             return true;
  107.         }
  108.         if (evt.id == Event.WINDOW_DESTROY) {
  109.             if (inAnApplet) {
  110.                 dispose();
  111.                 return true;
  112.             } else {
  113.                 System.exit(0);
  114.             }
  115.         }
  116.         return super.handleEvent(evt);
  117.     }
  118.  
  119.     public static void main(String args[]) {
  120.     GUIWindow window = new GUIWindow();
  121.         window.inAnApplet = false;
  122.  
  123.         //window.setTitle(window.getClass().getName() + " Application");
  124.         window.setTitle("The AWT Components");
  125.         window.pack();
  126.         window.show();
  127.     }
  128.  
  129. }
  130.  
  131. class MyCanvas extends Canvas {
  132.  
  133.     public void paint(Graphics g) {
  134.     int w = size().width;
  135.     int h = size().height;
  136.     g.drawRect(0, 0, w - 1, h - 1);
  137.     g.drawString("Canvas", (w - g.getFontMetrics().stringWidth("Canvas"))/2,
  138.               10);
  139.  
  140.     g.setFont(new Font("Helvetica", Font.PLAIN, 8));
  141.     g.drawLine(10,10, 100,100);
  142.     g.fillRect(9,9,3,3);
  143.     g.drawString("(10,10)", 13, 10);
  144.     g.fillRect(49,49,3,3);
  145.     g.drawString("(50,50)", 53, 50);
  146.     g.fillRect(99,99,3,3);
  147.     g.drawString("(100,100)", 103, 100);
  148.     }
  149.  
  150.     public Dimension minimumSize() {
  151.     return new Dimension(150,130);
  152.     }
  153.  
  154.     public Dimension preferredSize() {
  155.     return minimumSize();
  156.     }
  157. }
  158. </pre>
  159. </blockquote>
  160. <p>
  161. <hr size=4>
  162. <p>
  163. </body>
  164. </html>
  165.